package com.tanhua.server.controller;


import com.tanhua.model.vo.PageResult;
import com.tanhua.server.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/comments")
public class CommentController {

    @Autowired
    private CommentService commentService;

    /**
     * 查询动态评论列表
     * @param movementId
     * @param page
     * @param pagesize
     * @return
     */
    @GetMapping
    public ResponseEntity comments(String movementId,
                                   @RequestParam(defaultValue = "1") int page,
                                   @RequestParam(defaultValue = "10") int pagesize){
        PageResult pageResult = commentService.comments(page,pagesize,movementId);
        return ResponseEntity.ok(pageResult);
    }

    /**
     * 发布动态
     * @return
     */
    @PostMapping
    public ResponseEntity saveComments(@RequestBody Map map){
        String movementId = (String) map.get("movementId");
        String content = (String) map.get("comment");

        commentService.saveComments(movementId,content);

        return ResponseEntity.ok(null);
    }

    /**
     * 评论点赞
     * @return
     */
    @GetMapping("/{id}/like")
    public ResponseEntity commentLike(@PathVariable("id") String commentId){
        Integer countsLike = commentService.commentLike(commentId);

        return ResponseEntity.ok(countsLike);
    }

    /**
     * 评论取消点赞
     * @param commentId
     * @return
     */
    @GetMapping("/{id}/dislike")
    public ResponseEntity dislike(@PathVariable("id") String commentId){
        Integer countLike = commentService.dislike(commentId);

        return ResponseEntity.ok(countLike);
    }
}
